<?php
// +----------------------------------------------------------------------
// | Description: 订单管理
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-01-25 09:50:58
// +----------------------------------------------------------------------

namespace app\client\controller;

use app\common\controller\Rest;
use think\Db;

class Orders extends Rest
{
    public $modelName = 'Order';
    public function _initialize()
    {
        parent::_initialize();
    }

    public function save()
    {
        $res = self::$model->createData($this->param);
        $this->checkRes($res, $res, self::$model->getError());
    }

    public function update()
    {
        $sql = $this -> checkStatus($this->param); // 检验订单的状态修改是否符合要求

        self::$model -> startTrans();
        try{
            if($this -> isClient){
                $fields = 'OrderStatus,CloseReason,ReceiveDate,FinishDate';
            }else{
                $fields = true;
            }
            Db::query($sql);
            $res = self::$model->updateDataById($this->param, $this->param['id'], $fields);
            self::$model -> commit();
        }catch(\Exception $e){
            self::$model -> rollback();
            $this -> abort(false, $e->getMessage());
        }

        $this->checkRes($res, '编辑成功', self::$model->getError());
    }

    private function checkStatus($param)
    {
        $order = self::$model -> get($param['id']);
        // $this -> jsonData($order);
        if($this -> isClient){
            if(!($order['OrderStatus'] == 1 and $param['OrderStatus'] == 0) and !( $order['OrderStatus'] == 3 and $param['OrderStatus'] == 4) and !( $order['OrderStatus'] == 5 and $param['IsDelete'] ==  1 )  and !( $order['OrderStatus'] == 6 and $param['IsDelete'] ==  1 ) and !( $order['OrderStatus'] == 0 and $param['IsDelete'] ==  1 ) ) {
                $this -> abort(false, '非法请求: 订单状态设定异常');
            }
        }

        $newStatusName = $this -> parseStatus($param['OrderStatus']);
        $OldStatusName = $this -> parseStatus($order['OrderStatus']);

        $sql = "UPDATE shop_members SET $newStatusName = $newStatusName + 1, $OldStatusName = $OldStatusName - 1 WHERE Id = ".$param['UserId'];

        return $sql;
    }

    public function parseStatus($statusNum)
    {
        switch($statusNum){
            case 0:
                return 'OrderStatusZero';
            case 1:
                return 'OrderStatusOne';
            case 2:
                return 'OrderStatusTwo';
            case 3:
                return 'OrderStatusThree';
            case 4:
                return 'OrderStatusFour';
            case 5:
                return 'OrderStatusFive';
        }
    }

    public function OrderCount()
    {
        $this -> jsonData(self::$model -> OrderCount($this->param), self::$model -> getError());
    }

    /**
     * 库存回滚
     */
    public function RollbackStock()
    {

    }
}
